//
//  CMessageMgr.m
//  wanzhaoIM
//
//  Created by liuyuantao on 15/5/20.
//  Copyright (c) 2015年 liuyuantao. All rights reserved.
//

#import "CMessageMgr.h"
#import "FMDB.h"
#import "CFolderMgr.h"
#import "MicroDef.h"
#import "CMessageModel.h"
#import "NSData+Base64.h"

@implementation CMessageMgr


#warning 加载群时，需要当心群id为空的情况
//插入消息
-(BOOL)insertMessage:(CMessageModel *)message
{
    
    if(message == nil || M_ISSTRNULL(message.MessageId))
    {
        return NO;
    }
    
//    NSLog(@"%@",[[CFolderMgr shareInstance] personalDatabasePathRecently]);
    __block BOOL ret = NO;;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        NSString *result = [db stringForQuery:@"select TimeStamp from MESSAGE where MessageId = ?",message.MessageId];
        if (!M_ISSTRNULL(result)) {
            ret = YES;
        }
        if (ret) {
            
        } else {
            ret = [db executeUpdate:@"INSERT INTO MESSAGE(ServerID, MessageType, MessageContent, MessageUri, FileSize, SoundLength, FileCacheName, FileName, ReadStatus, SoundPlayStatus, MessageSendedStatus, GroupId,SenderCode,MineJid,FriendJid, TimeStamp , MessageId,MineUserId,FriendUserId,isMineSend,FriendUserName,MineUserName,IsShowInmessageList,Stream, SendStatus,yuLiuZiduan1, yuLiuZiduan2, yuLiuZiduan3) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", M_STOREVALUE(message.ServerID), M_STOREVALUE(message.MessageType), M_STOREVALUE(message.MessageContent) , M_STOREVALUE(message.MessageUri), M_STOREVALUE(message.FileSize), @(message.SoundLength), M_STOREVALUE(message.FileCacheName), M_STOREVALUE(message.FileName), message.ReadStatus?@1:@0, @(message.SoundPlayStatus), @(message.MessageSendedStatus), M_STOREVALUE(message.GroupId), M_STOREVALUE(message.SenderCode),M_STOREVALUE(message.MineJid),M_STOREVALUE(message.FriendJid),M_STOREVALUE(message.TimeStamp),M_STOREVALUE(message.MessageId),M_STOREVALUE(message.MineUserId),M_STOREVALUE(message.FriendUserId),message.isMineSend?@1:@0,M_STOREVALUE(message.FriendUserName),M_STOREVALUE(message.MineUserName),message.IsShowInmessageList?@1:@0,M_STOREVALUE(message.Stream),M_STOREVALUE(message.SendStatus),M_STOREVALUE(message.yuLiuZiduan1),@(message.yuLiuZiduan2),M_STOREVALUE(message.yuLiuZiduan3)];

        }
    }];
    return ret;
}
//批量插入数据
-(BOOL)batchInsertMessages:(NSArray *)messagList
{
    if(messagList == nil || [messagList count] <= 0)
    {
        return NO;
    }
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inTransaction:^(FMDatabase *db, BOOL *rollback) {
        for(CMessageModel *message in messagList)
        {
            ret = NO;
            if (message && !M_ISSTRNULL(message.MessageId)) {
                NSString *result = [db stringForQuery:@"select TimeStamp from MESSAGE where MessageId = ?",message.MessageId];
                if (!M_ISSTRNULL(result)) {
                    ret = YES;
                } else {
                    ret = [db executeUpdate:@"INSERT INTO MESSAGE(ServerID, MessageType, MessageContent, MessageUri, FileSize, SoundLength, FileCacheName, FileName, ReadStatus, SoundPlayStatus, MessageSendedStatus, GroupId,SenderCode,MineJid,FriendJid, TimeStamp , MessageId,MineUserId,FriendUserId,isMineSend,FriendUserName,MineUserName,IsShowInmessageList,Stream, SendStatus,yuLiuZiduan1, yuLiuZiduan2, yuLiuZiduan3) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", M_STOREVALUE(message.ServerID), M_STOREVALUE(message.MessageType), M_STOREVALUE(message.MessageContent) , M_STOREVALUE(message.MessageUri), M_STOREVALUE(message.FileSize), @(message.SoundLength), M_STOREVALUE(message.FileCacheName), M_STOREVALUE(message.FileName), message.ReadStatus?@1:@0, @(message.SoundPlayStatus), @(message.MessageSendedStatus), M_STOREVALUE(message.GroupId), M_STOREVALUE(message.SenderCode),M_STOREVALUE(message.MineJid),M_STOREVALUE(message.FriendJid),M_STOREVALUE(message.TimeStamp),M_STOREVALUE(message.MessageId),M_STOREVALUE(message.MineUserId),M_STOREVALUE(message.FriendUserId),message.isMineSend?@1:@0,M_STOREVALUE(message.FriendUserName),M_STOREVALUE(message.MineUserName),message.IsShowInmessageList?@1:@0,M_STOREVALUE(message.Stream),M_STOREVALUE(message.SendStatus),M_STOREVALUE(message.yuLiuZiduan1),@(message.yuLiuZiduan2),M_STOREVALUE(message.yuLiuZiduan3)];
                }
            }
            
            if(!ret)
            {
                *rollback = YES;
            }
        }
        
    }];
    return ret;
}

//加载历史消息列表

-(NSArray *)loadmessageList
{
    
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];
    
    NSString *sql = nil;
    
    sql = [NSString stringWithFormat:@"select * from MESSAGE where IsShowInmessageList = 1 AND TimeStamp in (select max(TimeStamp) as TimeStamp from MESSAGE group by FriendUserId)"];
    
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            CMessageModel *message = [[CMessageModel alloc] init];
            message.MessageIndex = [result longLongIntForColumn:@"MessageIndex"];
            message.ServerID = [result stringForColumn:@"ServerID"];
            message.MessageType = [result stringForColumn:@"MessageType"];
            message.MessageContent = [result stringForColumn:@"MessageContent"];
            message.MessageUri = [result stringForColumn:@"MessageUri"];
            message.FileSize = [result stringForColumn:@"FileSize"];
            message.SoundLength = [result intForColumn:@"SoundLength"];
            message.FileCacheName = [result stringForColumn:@"FileCacheName"];
            message.FileName = [result stringForColumn:@"FileName"];
            message.ReadStatus = [result boolForColumn:@"ReadStatus"];
            message.SoundPlayStatus = [result intForColumn:@"SoundPlayStatus"];
            message.MessageSendedStatus = [result intForColumn:@"MessageSendedStatus"];
            message.GroupId = [result stringForColumn:@"GroupId"];
            message.SenderCode = [result stringForColumn:@"SenderCode"];
            message.TimeStamp = [result stringForColumn:@"TimeStamp"];
            message.MineJid = [result stringForColumn:@"MineJid"];
            message.FriendJid = [result stringForColumn:@"FriendJid"];
            message.MessageId = [result stringForColumn:@"MessageId"];
            message.MineUserId = [result stringForColumn:@"MineUserId"];
            message.FriendUserId = [result stringForColumn:@"FriendUserId"];
            message.isMineSend = [result boolForColumn:@"isMineSend"];
            message.FriendUserName = [result stringForColumn:@"FriendUserName"];
            message.MineUserName = [result stringForColumn:@"MineUserName"];
            message.IsShowInmessageList = [result boolForColumn:@"IsShowInmessageList"];
            message.Stream = [result stringForColumn:@"Stream"];
            message.SendStatus = [result stringForColumn:@"SendStatus"];
            
            message.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            message.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            message.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];
            [array addObject:message];
            message = nil;
        }
        [result close];
    }];
    return array;
}

//- (void)ceshi
//{
//    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
//        NSString *result = [db stringForQuery:@"select max(TimeStamp) from MESSAGE"];
//        NSLog(@"%@＝＝＝＝＝＝",result);
////        while ([result next]) {
////            NSLog(@"%@",[result stringForColumn:@"TimeStamp"]);
////        }
//        
//    }];
//}


//获取所有的未读数
- (NSInteger)loadAllUnReadCount
{
    NSString *sql = nil;
    
    sql = [NSString stringWithFormat:@"select COUNT(*) from MESSAGE where ReadStatus = 0 AND isMineSend = 0"];
    __block NSInteger count;
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        count = [db intForQuery:sql];
        //        count = [result columnCount];
    }];
    return count;

}


#pragma mark 获取未查看群文件的群消息
- (NSArray *)loadNoLookFileMessageWithGroupId:(NSString *)groupId
{
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];
    
    NSString *sql = nil;
    
    sql = [NSString stringWithFormat:@"select MessageId from MESSAGE where GroupId =\'%@\' AND FileCacheName = \'%@\'",groupId,@"upLoadGroupFile"];
    
    //    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            NSString *index = [result stringForColumn:@"MessageId"];
            [array addObject:index];
            index = nil;
        }
        [result close];
    }];
    return array;
}

#pragma mark 更新群文件的查看状态
- (BOOL)updateGroupFileLookStatusWithGroupId:(NSString *)groupId array:(NSArray *)array
{
    if(array == nil || [array count] <= 0)
    {
        return NO;
    }
    
    __block BOOL ret = NO;
    
    
    [[FMShareDataBase shareDataBase].shareQueue inTransaction:^(FMDatabase *db, BOOL *rollback) {
        for(NSNumber *messageId in array)
        {
            ret = [db executeUpdate:@"UPDATE MESSAGE SET FileCacheName = ? where MessageId = ?",@"noUpLoadGroupFile",messageId];
            if(!ret)
            {
                *rollback = YES;
            }
        }
        
    }];
    
    return ret;

}
//获取未读数
- (NSInteger)loadUnReadCountWithMineUserId:(NSString *)MineUserId FriendUserId:(NSString *)FriendUserId
{
    NSString *sql = nil;
    
    sql = [NSString stringWithFormat:@"select COUNT(*) from MESSAGE where FriendUserId=\'%@\' AND MineUserId =\'%@\' AND ReadStatus = 0 AND isMineSend = 0",FriendUserId,MineUserId];
    __block NSInteger count;
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        count = [db intForQuery:sql];
//        count = [result columnCount];
        }];
    return count;
}

- (NSInteger)loadAllcountWithFriendId:(NSString *)friendId mineId:(NSString *)mineId
{
    if (M_ISSTRNULL(friendId) || M_ISSTRNULL(mineId)) {
        return 0;
    }
    NSString *sql = nil;
    
    sql = [NSString stringWithFormat:@"select COUNT(*) from MESSAGE where MineUserId =\'%@\' AND FriendUserId =\'%@\'",mineId,friendId];
    __block NSInteger count;
    //    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        count = [db intForQuery:sql];
        //        count = [result columnCount];
    }];
    return count;

}

//群聊天数据
- (NSInteger)loadAllcount:(NSString *)GroupId{
    
    if (M_ISSTRNULL(GroupId)) {
        return 0;
    }
    NSString *sql = nil;
    
    sql = [NSString stringWithFormat:@"select COUNT(*) from MESSAGE where GroupId =\'%@\'",GroupId];
    __block NSInteger count;
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        count = [db intForQuery:sql];
        //        count = [result columnCount];
    }];
    return count;
 
}
  

//加载聊天记录
-(NSArray *)loadMessageWithMineId:(NSString *)MineId FrinendId:(NSString *)FriendId Page:(NSString *)page PerPage:(NSString *)PerPage
{
    
    
    if(M_ISSTRNULL(FriendId) || M_ISSTRNULL(MineId))
    {
        return nil;
    }
    
    
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];
    
    NSString *sql = nil;
    
    sql = [NSString stringWithFormat:@"select * from MESSAGE where MineUserId =\'%@\' AND FriendUserId =\'%@\' order by TimeStamp  DESC LIMIT \'%@\',\'%@\'", MineId, FriendId,page,PerPage];
    
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            CMessageModel *message = [[CMessageModel alloc] init];
            message.MessageIndex = [result longLongIntForColumn:@"MessageIndex"];
            message.ServerID = [result stringForColumn:@"ServerID"];
            message.MessageType = [result stringForColumn:@"MessageType"];
            message.MessageContent = [result stringForColumn:@"MessageContent"];
            message.MessageUri = [result stringForColumn:@"MessageUri"];
            message.FileSize = [result stringForColumn:@"FileSize"];
            message.SoundLength = [result intForColumn:@"SoundLength"];
            message.FileCacheName = [result stringForColumn:@"FileCacheName"];
            message.FileName = [result stringForColumn:@"FileName"];
            message.ReadStatus = [result boolForColumn:@"ReadStatus"];
            message.SoundPlayStatus = [result intForColumn:@"SoundPlayStatus"];
            message.MessageSendedStatus = [result intForColumn:@"MessageSendedStatus"];
            message.GroupId = [result stringForColumn:@"GroupId"];
            message.SenderCode = [result stringForColumn:@"SenderCode"];
            message.TimeStamp = [result stringForColumn:@"TimeStamp"];
            message.MineUserId = [result stringForColumn:@"MineUserId"];
            message.FriendUserId = [result stringForColumn:@"FriendUserId"];
            message.MessageId = [result stringForColumn:@"MessageId"];
            message.MineUserId = [result stringForColumn:@"MineUserId"];
            message.FriendUserId = [result stringForColumn:@"FriendUserId"];
            message.isMineSend = [result boolForColumn:@"isMineSend"];
            message.FriendUserName = [result stringForColumn:@"FriendUserName"];
            message.MineUserName = [result stringForColumn:@"MineUserName"];
            message.IsShowInmessageList = [result boolForColumn:@"IsShowInmessageList"];
            message.Stream = [result stringForColumn:@"Stream"];
            message.SendStatus = [result stringForColumn:@"SendStatus"];
            
            message.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            message.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            message.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];

            [array addObject:message];
            message = nil;
        }
        [result close];
    }];
    return array;
}

//加载更多历史聊天信息
- (NSArray *)loadMoreHistoryMessageWithMineId:(NSString *)MineId FrinendId:(NSString *)FriendId Page:(NSString *)page PerPage:(NSString *)PerPage TimeStamp:(NSString *)TimeStamp
{
    if(M_ISSTRNULL(FriendId) || M_ISSTRNULL(MineId))
    {
        return nil;
    }
    
    
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];
    
    NSString *sql = nil;
    
    sql = [NSString stringWithFormat:@"select * from MESSAGE where MineUserId =\'%@\' AND FriendUserId =\'%@\' AND TimeStamp < \'%@\' order by TimeStamp  DESC LIMIT \'%@\',\'%@\'", MineId, FriendId,TimeStamp,page,PerPage];
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            CMessageModel *message = [[CMessageModel alloc] init];
            message.MessageIndex = [result longLongIntForColumn:@"MessageIndex"];
            message.ServerID = [result stringForColumn:@"ServerID"];
            message.MessageType = [result stringForColumn:@"MessageType"];
            message.MessageContent = [result stringForColumn:@"MessageContent"];
            message.MessageUri = [result stringForColumn:@"MessageUri"];
            message.FileSize = [result stringForColumn:@"FileSize"];
            message.SoundLength = [result intForColumn:@"SoundLength"];
            message.FileCacheName = [result stringForColumn:@"FileCacheName"];
            message.FileName = [result stringForColumn:@"FileName"];
            message.ReadStatus = [result boolForColumn:@"ReadStatus"];
            message.SoundPlayStatus = [result intForColumn:@"SoundPlayStatus"];
            message.MessageSendedStatus = [result intForColumn:@"MessageSendedStatus"];
            message.GroupId = [result stringForColumn:@"GroupId"];
            message.SenderCode = [result stringForColumn:@"SenderCode"];
            message.TimeStamp = [result stringForColumn:@"TimeStamp"];
            message.MineUserId = [result stringForColumn:@"MineUserId"];
            message.FriendUserId = [result stringForColumn:@"FriendUserId"];
            message.MessageId = [result stringForColumn:@"MessageId"];
            message.MineUserId = [result stringForColumn:@"MineUserId"];
            message.FriendUserId = [result stringForColumn:@"FriendUserId"];
            message.isMineSend = [result boolForColumn:@"isMineSend"];
            message.FriendUserName = [result stringForColumn:@"FriendUserName"];
            message.MineUserName = [result stringForColumn:@"MineUserName"];
            message.IsShowInmessageList = [result boolForColumn:@"IsShowInmessageList"];
            message.Stream = [result stringForColumn:@"Stream"];
            message.SendStatus = [result stringForColumn:@"SendStatus"];
            
            message.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            message.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            message.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];

            
            [array addObject:message];
            message = nil;
        }
        [result close];
    }];
    return array;

}

//加载未读消息
-(NSArray *)loadMessageWithMineId:(NSString *)MineId FrinendId:(NSString *)FriendId TimeStamp:(NSString *)TimeStamp
{
    if(M_ISSTRNULL(MineId) || M_ISSTRNULL(FriendId))
    {
        return nil;
    }
    
    
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];
    
    NSString *sql = nil;
    
    sql = [NSString stringWithFormat:@"select * from MESSAGE where (MineUserId =\'%@\' AND FriendUserId =\'%@\' AND TimeStamp > \'%@\') order by TimeStamp  ASC", MineId, FriendId,TimeStamp];
    
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            CMessageModel *message = [[CMessageModel alloc] init];
            message.MessageIndex = [result longLongIntForColumn:@"MessageIndex"];
            message.ServerID = [result stringForColumn:@"ServerID"];
            message.MessageType = [result stringForColumn:@"MessageType"];
            message.MessageContent = [result stringForColumn:@"MessageContent"];
            message.MessageUri = [result stringForColumn:@"MessageUri"];
            message.FileSize = [result stringForColumn:@"FileSize"];
            message.SoundLength = [result intForColumn:@"SoundLength"];
            message.FileCacheName = [result stringForColumn:@"FileCacheName"];
            message.FileName = [result stringForColumn:@"FileName"];
            message.ReadStatus = [result boolForColumn:@"ReadStatus"];
             message.SoundPlayStatus = [result intForColumn:@"SoundPlayStatus"];
            message.MessageSendedStatus = [result intForColumn:@"MessageSendedStatus"];
            message.GroupId = [result stringForColumn:@"GroupId"];
            message.SenderCode = [result stringForColumn:@"SenderCode"];
            message.TimeStamp = [result stringForColumn:@"TimeStamp"];
            message.FriendJid = [result stringForColumn:@"FriendJid"];
            message.MineJid = [result stringForColumn:@"MineJid"];
            message.MessageId = [result stringForColumn:@"MessageId"];
            message.FriendUserId = [result stringForColumn:@"FriendUserId"];
            message.MineUserId = [result stringForColumn:@"MineUserId"];
            message.isMineSend = [result boolForColumn:@"isMineSend"];
            message.FriendUserName = [result stringForColumn:@"FriendUserName"];
            message.MineUserName = [result stringForColumn:@"MineUserName"];
            message.IsShowInmessageList = [result boolForColumn:@"IsShowInmessageList"];
            message.Stream = [result stringForColumn:@"Stream"];
            message.SendStatus = [result stringForColumn:@"SendStatus"];
            
            message.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            message.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            message.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];

            
            [array addObject:message];
            message = nil;
        }
        [result close];
    }];
    return array;
    
}

//更新消息的已读未读状态
- (void)updateMessageReadStatusWithMineUserId:(NSString *)MineUserId FriendUserId:(NSString *)FriendUserId Status:(BOOL)ReadStatus
{
    if(M_ISSTRNULL(MineUserId) || M_ISSTRNULL(FriendUserId))
    {
        return;
    }

//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    
    
    
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"UPDATE MESSAGE SET  ReadStatus = ? where MineUserId = ? AND FriendUserId = ?", ReadStatus?@1:@0,M_STOREVALUE(MineUserId),M_STOREVALUE(FriendUserId)];
        
    }];
    
}

- (void)delMessageWithMineUserId:(NSString *)MineUserId FriendUserId:(NSString *)FriendUserId
{
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        
        ret = [db executeUpdate:@"delete from MESSAGE where MineUserId = ? AND FriendUserId = ?",MineUserId,FriendUserId];
    }];
}


//加载群聊天记录
-(NSArray *)loadGroupMessageWithGroupId:(NSString *)GroupId Page:(NSString *)page PerPage:(NSString *)PerPage
{
    if(M_ISSTRNULL(GroupId))
    {
        return nil;
    }
    
    
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];
    
    NSString *sql = nil;
    
    sql = [NSString stringWithFormat:@"select * from MESSAGE where GroupId =\'%@\' order by TimeStamp DESC LIMIT %@,%@", GroupId,page,PerPage];
    
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            CMessageModel *message = [[CMessageModel alloc] init];
            message.MessageIndex = [result longLongIntForColumn:@"MessageIndex"];
            message.ServerID = [result stringForColumn:@"ServerID"];
            message.MessageType = [result stringForColumn:@"MessageType"];
            message.MessageContent = [result stringForColumn:@"MessageContent"];
            message.MessageUri = [result stringForColumn:@"MessageUri"];
            message.FileSize = [result stringForColumn:@"FileSize"];
            message.SoundLength = [result intForColumn:@"SoundLength"];
            message.FileCacheName = [result stringForColumn:@"FileCacheName"];
            message.FileName = [result stringForColumn:@"FileName"];
            message.ReadStatus = [result boolForColumn:@"ReadStatus"];
             message.SoundPlayStatus = [result intForColumn:@"SoundPlayStatus"];
            message.MessageSendedStatus = [result intForColumn:@"MessageSendedStatus"];
            message.GroupId = [result stringForColumn:@"GroupId"];
            message.SenderCode = [result stringForColumn:@"SenderCode"];
            message.TimeStamp = [result stringForColumn:@"TimeStamp"];
            message.MineUserId = [result stringForColumn:@"MineUserId"];
            message.FriendUserId = [result stringForColumn:@"FriendUserId"];
            message.MessageId = [result stringForColumn:@"MessageId"];
            message.MineUserId = [result stringForColumn:@"MineUserId"];
            message.FriendUserId = [result stringForColumn:@"FriendUserId"];
            message.isMineSend = [result boolForColumn:@"isMineSend"];
            message.FriendUserName = [result stringForColumn:@"FriendUserName"];
            message.MineUserName = [result stringForColumn:@"MineUserName"];
            message.IsShowInmessageList = [result boolForColumn:@"IsShowInmessageList"];
            message.Stream = [result stringForColumn:@"Stream"];
            message.SendStatus = [result stringForColumn:@"SendStatus"];
            
            message.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            message.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            message.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];

            
            [array addObject:message];
            
            message = nil;
        }
        [result close];
    }];
    return array;
}

//加载更多群聊天记录
- (NSArray *)loadMoreGroupHistoryMessageWithGroupId:(NSString *)GroupId Page:(NSString *)page PerPage:(NSString *)PerPage TimeStamp:(NSString *)TimeStamp
{
    if(M_ISSTRNULL(GroupId))
    {
        return nil;
    }
    
    
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];
    
    NSString *sql = nil;
    
    sql = [NSString stringWithFormat:@"select * from MESSAGE where GroupId = \'%@\' AND TimeStamp < \'%@\' order by TimeStamp DESC LIMIT \'%@\',\'%@\'", GroupId,TimeStamp,page,PerPage];
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            CMessageModel *message = [[CMessageModel alloc] init];
            message.MessageIndex = [result longLongIntForColumn:@"MessageIndex"];
            message.ServerID = [result stringForColumn:@"ServerID"];
            message.MessageType = [result stringForColumn:@"MessageType"];
            message.MessageContent = [result stringForColumn:@"MessageContent"];
            message.MessageUri = [result stringForColumn:@"MessageUri"];
            message.FileSize = [result stringForColumn:@"FileSize"];
            message.SoundLength = [result intForColumn:@"SoundLength"];
            message.FileCacheName = [result stringForColumn:@"FileCacheName"];
            message.FileName = [result stringForColumn:@"FileName"];
            message.ReadStatus = [result boolForColumn:@"ReadStatus"];
            message.SoundPlayStatus = [result intForColumn:@"SoundPlayStatus"];
            message.MessageSendedStatus = [result intForColumn:@"MessageSendedStatus"];
            message.GroupId = [result stringForColumn:@"GroupId"];
            message.SenderCode = [result stringForColumn:@"SenderCode"];
            message.TimeStamp = [result stringForColumn:@"TimeStamp"];
            message.MineUserId = [result stringForColumn:@"MineUserId"];
            message.FriendUserId = [result stringForColumn:@"FriendUserId"];
            message.MessageId = [result stringForColumn:@"MessageId"];
            message.MineUserId = [result stringForColumn:@"MineUserId"];
            message.FriendUserId = [result stringForColumn:@"FriendUserId"];
            message.isMineSend = [result boolForColumn:@"isMineSend"];
            message.FriendUserName = [result stringForColumn:@"FriendUserName"];
            message.MineUserName = [result stringForColumn:@"MineUserName"];
            message.IsShowInmessageList = [result boolForColumn:@"IsShowInmessageList"];
            message.Stream = [result stringForColumn:@"Stream"];
            message.SendStatus = [result stringForColumn:@"SendStatus"];
            
            message.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            message.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            message.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];

            
            [array addObject:message];
            message = nil;
        }
        [result close];
    }];
    return array;
    
}
- (NSArray *)loadMoreHistoryMessageWithFriendId:(NSString *)friendId mineId:(NSString *)mineId Page:(NSString *)page PerPage:(NSString *)PerPage TimeStamp:(NSString *)TimeStamp
{
    if(M_ISSTRNULL(friendId) || M_ISSTRNULL(mineId))
    {
        return nil;
    }
    
    
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];
    
    NSString *sql = nil;
    
    sql = [NSString stringWithFormat:@"select * from MESSAGE where MineUserId =\'%@\' AND FriendUserId =\'%@\' AND TimeStamp < \'%@\' order by TimeStamp DESC LIMIT \'%@\',\'%@\'", mineId,friendId,TimeStamp,page,PerPage];
    //    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            CMessageModel *message = [[CMessageModel alloc] init];
            message.MessageIndex = [result longLongIntForColumn:@"MessageIndex"];
            message.ServerID = [result stringForColumn:@"ServerID"];
            message.MessageType = [result stringForColumn:@"MessageType"];
            message.MessageContent = [result stringForColumn:@"MessageContent"];
            message.MessageUri = [result stringForColumn:@"MessageUri"];
            message.FileSize = [result stringForColumn:@"FileSize"];
            message.SoundLength = [result intForColumn:@"SoundLength"];
            message.FileCacheName = [result stringForColumn:@"FileCacheName"];
            message.FileName = [result stringForColumn:@"FileName"];
            message.ReadStatus = [result boolForColumn:@"ReadStatus"];
            message.SoundPlayStatus = [result intForColumn:@"SoundPlayStatus"];
            message.MessageSendedStatus = [result intForColumn:@"MessageSendedStatus"];
            message.GroupId = [result stringForColumn:@"GroupId"];
            message.SenderCode = [result stringForColumn:@"SenderCode"];
            message.TimeStamp = [result stringForColumn:@"TimeStamp"];
            message.MineUserId = [result stringForColumn:@"MineUserId"];
            message.FriendUserId = [result stringForColumn:@"FriendUserId"];
            message.MessageId = [result stringForColumn:@"MessageId"];
            message.MineUserId = [result stringForColumn:@"MineUserId"];
            message.FriendUserId = [result stringForColumn:@"FriendUserId"];
            message.isMineSend = [result boolForColumn:@"isMineSend"];
            message.FriendUserName = [result stringForColumn:@"FriendUserName"];
            message.MineUserName = [result stringForColumn:@"MineUserName"];
            message.IsShowInmessageList = [result boolForColumn:@"IsShowInmessageList"];
            message.Stream = [result stringForColumn:@"Stream"];
            message.SendStatus = [result stringForColumn:@"SendStatus"];
            
            message.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            message.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            message.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];

            
            [array addObject:message];
            message = nil;
        }
        [result close];
    }];
    return array;

}
//加载更早的个人聊天信息
- (NSArray *)loadreduceMoreGroupHistoryMessageWithFriendId:(NSString *)frienId mineId:(NSString *)mineId Page:(NSString *)page PerPage:(NSString *)PerPage TimeStamp:(NSString *)TimeStamp
{
    if(M_ISSTRNULL(mineId) || M_ISSTRNULL(frienId))
    {
        return nil;
    }
    
    
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];
    
    NSString *sql = nil;
    
    sql = [NSString stringWithFormat:@"select * from MESSAGE where MineUserId =\'%@\' AND FriendUserId =\'%@\' AND TimeStamp > \'%@\' order by TimeStamp asc LIMIT \'%@\',\'%@\'", mineId,frienId,TimeStamp,page,PerPage];
    //    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            CMessageModel *message = [[CMessageModel alloc] init];
            message.MessageIndex = [result longLongIntForColumn:@"MessageIndex"];
            message.ServerID = [result stringForColumn:@"ServerID"];
            message.MessageType = [result stringForColumn:@"MessageType"];
            message.MessageContent = [result stringForColumn:@"MessageContent"];
            message.MessageUri = [result stringForColumn:@"MessageUri"];
            message.FileSize = [result stringForColumn:@"FileSize"];
            message.SoundLength = [result intForColumn:@"SoundLength"];
            message.FileCacheName = [result stringForColumn:@"FileCacheName"];
            message.FileName = [result stringForColumn:@"FileName"];
            message.ReadStatus = [result boolForColumn:@"ReadStatus"];
            message.SoundPlayStatus = [result intForColumn:@"SoundPlayStatus"];
            message.MessageSendedStatus = [result intForColumn:@"MessageSendedStatus"];
            message.GroupId = [result stringForColumn:@"GroupId"];
            message.SenderCode = [result stringForColumn:@"SenderCode"];
            message.TimeStamp = [result stringForColumn:@"TimeStamp"];
            message.MineUserId = [result stringForColumn:@"MineUserId"];
            message.FriendUserId = [result stringForColumn:@"FriendUserId"];
            message.MessageId = [result stringForColumn:@"MessageId"];
            message.MineUserId = [result stringForColumn:@"MineUserId"];
            message.FriendUserId = [result stringForColumn:@"FriendUserId"];
            message.isMineSend = [result boolForColumn:@"isMineSend"];
            message.FriendUserName = [result stringForColumn:@"FriendUserName"];
            message.MineUserName = [result stringForColumn:@"MineUserName"];
            message.IsShowInmessageList = [result boolForColumn:@"IsShowInmessageList"];
            message.Stream = [result stringForColumn:@"Stream"];
            message.SendStatus = [result stringForColumn:@"SendStatus"];
            
            message.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            message.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            message.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];

            
            [array addObject:message];
            message = nil;
        }
        [result close];
    }];
    return array;

}
//群消息
- (NSArray *)loadreduceMoreGroupHistoryMessageWithGroupId:(NSString *)GroupId Page:(NSString *)page PerPage:(NSString *)PerPage TimeStamp:(NSString *)TimeStamp
{
    if(M_ISSTRNULL(GroupId))
    {
        return nil;
    }
    
    
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];
    
    NSString *sql = nil;
    
    sql = [NSString stringWithFormat:@"select * from MESSAGE where GroupId = \'%@\' AND TimeStamp > \'%@\' order by TimeStamp asc LIMIT \'%@\',\'%@\'", GroupId,TimeStamp,page,PerPage];
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            CMessageModel *message = [[CMessageModel alloc] init];
            message.MessageIndex = [result longLongIntForColumn:@"MessageIndex"];
            message.ServerID = [result stringForColumn:@"ServerID"];
            message.MessageType = [result stringForColumn:@"MessageType"];
            message.MessageContent = [result stringForColumn:@"MessageContent"];
            message.MessageUri = [result stringForColumn:@"MessageUri"];
            message.FileSize = [result stringForColumn:@"FileSize"];
            message.SoundLength = [result intForColumn:@"SoundLength"];
            message.FileCacheName = [result stringForColumn:@"FileCacheName"];
            message.FileName = [result stringForColumn:@"FileName"];
            message.ReadStatus = [result boolForColumn:@"ReadStatus"];
            message.SoundPlayStatus = [result intForColumn:@"SoundPlayStatus"];
            message.MessageSendedStatus = [result intForColumn:@"MessageSendedStatus"];
            message.GroupId = [result stringForColumn:@"GroupId"];
            message.SenderCode = [result stringForColumn:@"SenderCode"];
            message.TimeStamp = [result stringForColumn:@"TimeStamp"];
            message.MineUserId = [result stringForColumn:@"MineUserId"];
            message.FriendUserId = [result stringForColumn:@"FriendUserId"];
            message.MessageId = [result stringForColumn:@"MessageId"];
            message.MineUserId = [result stringForColumn:@"MineUserId"];
            message.FriendUserId = [result stringForColumn:@"FriendUserId"];
            message.isMineSend = [result boolForColumn:@"isMineSend"];
            message.FriendUserName = [result stringForColumn:@"FriendUserName"];
            message.MineUserName = [result stringForColumn:@"MineUserName"];
            message.IsShowInmessageList = [result boolForColumn:@"IsShowInmessageList"];
            message.Stream = [result stringForColumn:@"Stream"];
            message.SendStatus = [result stringForColumn:@"SendStatus"];
            
            message.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            message.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            message.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];

            
            [array addObject:message];
            message = nil;
        }
        [result close];
    }];
    return array;
    
}
//获取群未读数
- (NSInteger)loadGroupUnReadCountWithGroupdId:(NSString *)GroupId
{
    if(M_ISSTRNULL(GroupId))
    {
        return 0;
    }

    NSString *sql = nil;
    
    sql = [NSString stringWithFormat:@"select COUNT(*) from MESSAGE where GroupId =\'%@\' AND ReadStatus = 0 AND isMineSend = 0",GroupId];
    __block NSInteger count;
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        count = [db intForQuery:sql];
        //        count = [result columnCount];
    }];
    return count;
}
//加载未读群消息
-(NSArray *)loadGroupMessageWithGroupId:(NSString *)GroupId TimeStamp:(NSString *)TimeStamp
{
    if(M_ISSTRNULL(GroupId))
    {
        return nil;
    }
    
    
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];
    
    NSString *sql = nil;
    
    sql = [NSString stringWithFormat:@"select * from Message where (GroupId =\'%@\' AND TimeStamp > \'%@\') order by TimeStamp  ASC", GroupId,TimeStamp];
    
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            CMessageModel *message = [[CMessageModel alloc] init];
            message.MessageIndex = [result longLongIntForColumn:@"MessageIndex"];
            message.ServerID = [result stringForColumn:@"ServerID"];
            message.MessageType = [result stringForColumn:@"MessageType"];
            message.MessageContent = [result stringForColumn:@"MessageContent"];
            message.MessageUri = [result stringForColumn:@"MessageUri"];
            message.FileSize = [result stringForColumn:@"FileSize"];
            message.SoundLength = [result intForColumn:@"SoundLength"];
            message.FileCacheName = [result stringForColumn:@"FileCacheName"];
            message.FileName = [result stringForColumn:@"FileName"];
            message.ReadStatus = [result boolForColumn:@"ReadStatus"];
             message.SoundPlayStatus = [result intForColumn:@"SoundPlayStatus"];
            message.MessageSendedStatus = [result intForColumn:@"MessageSendedStatus"];
            message.GroupId = [result stringForColumn:@"GroupId"];
            message.SenderCode = [result stringForColumn:@"SenderCode"];
            message.TimeStamp = [result stringForColumn:@"TimeStamp"];
            message.FriendJid = [result stringForColumn:@"FriendJid"];
            message.MineJid = [result stringForColumn:@"MineJid"];
            message.MessageId = [result stringForColumn:@"MessageId"];
            message.FriendUserId = [result stringForColumn:@"FriendUserId"];
            message.MineUserId = [result stringForColumn:@"MineUserId"];
            message.isMineSend = [result boolForColumn:@"isMineSend"];
            message.FriendUserName = [result stringForColumn:@"FriendUserName"];
            message.MineUserName = [result stringForColumn:@"MineUserName"];
            message.IsShowInmessageList = [result boolForColumn:@"IsShowInmessageList"];
            message.Stream = [result stringForColumn:@"Stream"];
            message.SendStatus = [result stringForColumn:@"SendStatus"];
            
            message.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            message.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            message.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];

            
            [array addObject:message];
            message = nil;
        }
        [result close];
    }];
    return array;
    
}

//更新群消息的已读未读状态
- (void)updateGroupMessageReadStatusWithGroupId:(NSString *)GroupId Status:(BOOL)ReadStatus
{
    if(M_ISSTRNULL(GroupId))
    {
        return;
    }

//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    
    
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"UPDATE MESSAGE SET  ReadStatus = ? where GroupId = ?", ReadStatus?@1:@0,M_STOREVALUE(GroupId)];
        
    }];
    
}


//删除群消息
- (void)delGroupMessageWithGroupId:(NSString *)GroupId
{
    
    if(M_ISSTRNULL(GroupId))
    {
        return;
    }

//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        
        ret = [db executeUpdate:@"delete from MESSAGE where GroupId = ?",GroupId];
    }];
}

- (BOOL)updateMessageWithIsShowInMessageList:(BOOL)IsShowInMessageList
{
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    
    
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"UPDATE MESSAGE SET ReadStatus = ?, IsShowInmessageList = ?", @1,IsShowInMessageList?@1:@0];
        
    }];
    return ret;
}

- (BOOL)updateMessageWithIsShowInMessageList:(BOOL)IsShowInMessageList FriendId:(NSString *)FriendId MineId:(NSString *)MineId
{
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    
    
       [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"UPDATE MESSAGE SET  ReadStatus = ?, IsShowInmessageList = ? where MineUserId = ? AND FriendUserId = ?", @1,IsShowInMessageList?@1:@0,M_STOREVALUE(MineId),M_STOREVALUE(FriendId)];
        
    }];
    return ret;

}
- (BOOL)updateMessageWithIsShowInMessageList:(BOOL)IsShowInMessageList groupId:(NSString *)GroupId
{
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    
    
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"UPDATE MESSAGE SET  ReadStatus = ?, IsShowInmessageList = ? where GroupId = ?",@1, IsShowInMessageList?@1:@0,M_STOREVALUE(GroupId)];
        
    }];
    return ret;

}

////根据 messageIndex请求消息
-(CMessageModel *)loadMessageByMessageId:(NSString *)messageId
{
    if(M_ISSTRNULL(messageId))
    {
        return nil;
    }
    NSString *sql = nil;
    __block CMessageModel *message = nil;
    sql = [NSString stringWithFormat:@"select * from MESSAGE where MessageId = \'%@\'", messageId];
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            message = [[CMessageModel alloc] init];
            message.MessageIndex = [result longLongIntForColumn:@"MessageIndex"];
            message.ServerID = [result stringForColumn:@"ServerID"];
            message.MessageType = [result stringForColumn:@"MessageType"];
            message.MessageContent = [result stringForColumn:@"MessageContent"];
            message.MessageUri = [result stringForColumn:@"MessageUri"];
            message.FileSize = [result stringForColumn:@"FileSize"];
            message.SoundLength = [result intForColumn:@"SoundLength"];
            message.FileCacheName = [result stringForColumn:@"FileCacheName"];
            message.FileName = [result stringForColumn:@"FileName"];
            message.ReadStatus = [result boolForColumn:@"ReadStatus"];
            message.SoundPlayStatus = [result intForColumn:@"SoundPlayStatus"];
            message.MessageSendedStatus = [result intForColumn:@"MessageSendedStatus"];
            message.GroupId = [result stringForColumn:@"GroupId"];
            message.SenderCode = [result stringForColumn:@"SenderCode"];
            message.TimeStamp = [result stringForColumn:@"TimeStamp"];
            message.FriendJid = [result stringForColumn:@"FriendJid"];
            message.MineJid = [result stringForColumn:@"MineJid"];
            message.MessageId = [result stringForColumn:@"MessageId"];
            message.FriendUserId = [result stringForColumn:@"FriendUserId"];
            message.MineUserId = [result stringForColumn:@"MineUserId"];
            message.isMineSend = [result boolForColumn:@"isMineSend"];
            message.FriendUserName = [result stringForColumn:@"FriendUserName"];
            message.MineUserName = [result stringForColumn:@"MineUserName"];
            message.IsShowInmessageList = [result boolForColumn:@"IsShowInmessageList"];
            message.Stream = [result stringForColumn:@"Stream"];
            message.SendStatus = [result stringForColumn:@"SendStatus"];
            
            message.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            message.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            message.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];

            
        }
        [result close];
    }];
    return message;
}


//更新消息的已读/未读状态
-(BOOL)updateMessageReadStatus:(CMessageModel *)message readStatus:(BOOL)readStatus
{
    if(message == nil)
    {
        return NO;
    }
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"UPDATE Message SET  ReadStatus = ? where MessageId = ?", readStatus?@1:@0,message.MessageId];
        
    }];
    return ret;
}
//更新消息的已读 未读状态
-(BOOL)updateMessageReadStatusByFrom:(NSString *)fromUser GroupId:(NSString *)groupId  readStatus:(BOOL)readStatus
{
    
    if(M_ISSTRNULL(fromUser))
    {
        return NO;
    }
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"UPDATE Message SET  ReadStatus = ? where SenderCode = ? AND GroupId = ? ", readStatus?@1:@0, M_STOREVALUE(fromUser), M_STOREVALUE(groupId)];
    }];
    return ret;
}

//删除所有的 消息
-(BOOL)delAllMessages
{
    
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        
        ret = [db executeUpdate:@"delete from MESSAGE"];
    }];
    return ret;
}


//删除单条 消息
-(BOOL)delMessages:(CMessageModel *)message
{
    if(message == nil)
    {
        return NO;
    }
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        
        ret = [db executeUpdate:@"delete from MESSAGE where MessageId = ?", message.MessageId];
    }];
    return ret;
}

-(BOOL)delMessageWithMessageId:(NSString *)messageId
{
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        
        ret = [db executeUpdate:@"delete from MESSAGE where MessageId = ?", messageId];
    }];
    return ret;
}


//更新消息
-(BOOL)updateMessage:(CMessageModel *)message
{
    
    if(message == nil)
    {
        return NO;
    }
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"UPDATE MESSAGE SET ServerID = ?, MessageType = ?, MessageContent = ?, MessageUri = ?, FileSize = ?, SoundLength = ?, FileCacheName = ?, FileName = ?, ReadStatus = ?, SoundPlayStatus = ?, MessageSendedStatus = ?, GroupId = ?, SenderCode = ? ,MineJid = ?,FriendJid = ?, TimeStamp = ?,MineUserId = ?,FriendUserId = ?,isMineSend = ?,FriendUserName = ?,MineUserName = ?,IsShowInmessageList = ?, Stream = ?, SendStatus = ? where MessageId = ?",M_STOREVALUE(message.ServerID), M_STOREVALUE(message.MessageType), M_STOREVALUE(message.MessageContent) , M_STOREVALUE(message.MessageUri), M_STOREVALUE(message.FileSize), @(message.SoundLength), M_STOREVALUE(message.FileCacheName), M_STOREVALUE(message.FileName), message.ReadStatus?@1:@0, @(message.SoundPlayStatus), @(message.MessageSendedStatus), M_STOREVALUE(message.GroupId), M_STOREVALUE(message.SenderCode),M_STOREVALUE(message.MineJid),M_STOREVALUE(message.FriendJid),M_STOREVALUE(message.TimeStamp),M_STOREVALUE(message.MineUserId),M_STOREVALUE(message.FriendUserId),message.isMineSend?@1:@0,M_STOREVALUE(message.FriendUserName),M_STOREVALUE(message.MineUserName),message.IsShowInmessageList?@1:@0,M_STOREVALUE(message.Stream),M_STOREVALUE(message.SendStatus),message.MessageId];
    }];
    return ret;
}


-(NSArray *)loadAllUnReadMessageWithGroupdId:(NSString *)GroupId
{
    
    if(M_ISSTRNULL(GroupId))
    {
        return nil;
    }
    
    
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];

    NSString *sql = nil;
    
    sql = [NSString stringWithFormat:@"select * from MESSAGE where GroupId =\'%@\' AND ReadStatus = 0 AND isMineSend = 0 order by TimeStamp DESC",GroupId];
    
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            CMessageModel *message = [[CMessageModel alloc] init];
            message.MessageIndex = [result longLongIntForColumn:@"MessageIndex"];
            message.ServerID = [result stringForColumn:@"ServerID"];
            message.MessageType = [result stringForColumn:@"MessageType"];
            message.MessageContent = [result stringForColumn:@"MessageContent"];
            message.MessageUri = [result stringForColumn:@"MessageUri"];
            message.FileSize = [result stringForColumn:@"FileSize"];
            message.SoundLength = [result intForColumn:@"SoundLength"];
            message.FileCacheName = [result stringForColumn:@"FileCacheName"];
            message.FileName = [result stringForColumn:@"FileName"];
            message.ReadStatus = [result boolForColumn:@"ReadStatus"];
            message.SoundPlayStatus = [result intForColumn:@"SoundPlayStatus"];
            message.MessageSendedStatus = [result intForColumn:@"MessageSendedStatus"];
            message.GroupId = [result stringForColumn:@"GroupId"];
            message.SenderCode = [result stringForColumn:@"SenderCode"];
            message.TimeStamp = [result stringForColumn:@"TimeStamp"];
            message.FriendJid = [result stringForColumn:@"FriendJid"];
            message.MineJid = [result stringForColumn:@"MineJid"];
            message.MessageId = [result stringForColumn:@"MessageId"];
            message.FriendUserId = [result stringForColumn:@"FriendUserId"];
            message.MineUserId = [result stringForColumn:@"MineUserId"];
            message.isMineSend = [result boolForColumn:@"isMineSend"];
            message.FriendUserName = [result stringForColumn:@"FriendUserName"];
            message.MineUserName = [result stringForColumn:@"MineUserName"];
            message.IsShowInmessageList = [result boolForColumn:@"IsShowInmessageList"];
            message.Stream = [result stringForColumn:@"Stream"];
            message.SendStatus = [result stringForColumn:@"SendStatus"];
            message.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            message.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            message.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];

            
            [array addObject:message];
            message = nil;
        }
        [result close];
        //        count = [result columnCount];
    }];
    return array;

}

- (NSArray *)loadUnReadMessageWithMineUserId:(NSString *)MineUserId FriendUserId:(NSString *)FriendUserId beginTimeStamp:(NSString *)beginTimeStamp endTimeStamp:(NSString *)endTimeStamp
{
    if(M_ISSTRNULL(MineUserId) || M_ISSTRNULL(FriendUserId))
    {
        return nil;
    }
    
    
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];
    
    NSString *sql = nil;
    
    sql = [NSString stringWithFormat:@"select * from MESSAGE where FriendUserId=\'%@\' AND MineUserId =\'%@\' AND TimeStamp >= \'%@\' AND TimeStamp < \'%@\' order by TimeStamp  DESC",FriendUserId,MineUserId,beginTimeStamp,endTimeStamp];
    
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            CMessageModel *message = [[CMessageModel alloc] init];
            message.MessageIndex = [result longLongIntForColumn:@"MessageIndex"];
            message.ServerID = [result stringForColumn:@"ServerID"];
            message.MessageType = [result stringForColumn:@"MessageType"];
            message.MessageContent = [result stringForColumn:@"MessageContent"];
            message.MessageUri = [result stringForColumn:@"MessageUri"];
            message.FileSize = [result stringForColumn:@"FileSize"];
            message.SoundLength = [result intForColumn:@"SoundLength"];
            message.FileCacheName = [result stringForColumn:@"FileCacheName"];
            message.FileName = [result stringForColumn:@"FileName"];
            message.ReadStatus = [result boolForColumn:@"ReadStatus"];
            message.SoundPlayStatus = [result intForColumn:@"SoundPlayStatus"];
            message.MessageSendedStatus = [result intForColumn:@"MessageSendedStatus"];
            message.GroupId = [result stringForColumn:@"GroupId"];
            message.SenderCode = [result stringForColumn:@"SenderCode"];
            message.TimeStamp = [result stringForColumn:@"TimeStamp"];
            message.FriendJid = [result stringForColumn:@"FriendJid"];
            message.MineJid = [result stringForColumn:@"MineJid"];
            message.MessageId = [result stringForColumn:@"MessageId"];
            message.FriendUserId = [result stringForColumn:@"FriendUserId"];
            message.MineUserId = [result stringForColumn:@"MineUserId"];
            message.isMineSend = [result boolForColumn:@"isMineSend"];
            message.FriendUserName = [result stringForColumn:@"FriendUserName"];
            message.MineUserName = [result stringForColumn:@"MineUserName"];
            message.IsShowInmessageList = [result boolForColumn:@"IsShowInmessageList"];
            message.Stream = [result stringForColumn:@"Stream"];
            message.SendStatus = [result stringForColumn:@"SendStatus"];
            
            
            message.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            message.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            message.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];

            
            [array addObject:message];
            message = nil;
        }
        [result close];
        //        count = [result columnCount];
    }];
    return array;

}

- (NSArray *)loadGroupUnReadMessageWithGroupdId:(NSString *)GroupId beginTimeStamp:(NSString *)beginTimeStamp endTimeStamp:(NSString *)endTimeStamp
{
    if(M_ISSTRNULL(GroupId))
    {
        return nil;
    }
    
    
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];
    
    NSString *sql = nil;
    
    sql = [NSString stringWithFormat:@"select * from MESSAGE where GroupId =\'%@\' AND TimeStamp >= \'%@\' AND TimeStamp < \'%@\' order by TimeStamp  DESC",GroupId,beginTimeStamp,endTimeStamp];
    
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            CMessageModel *message = [[CMessageModel alloc] init];
            message.MessageIndex = [result longLongIntForColumn:@"MessageIndex"];
            message.ServerID = [result stringForColumn:@"ServerID"];
            message.MessageType = [result stringForColumn:@"MessageType"];
            message.MessageContent = [result stringForColumn:@"MessageContent"];
            message.MessageUri = [result stringForColumn:@"MessageUri"];
            message.FileSize = [result stringForColumn:@"FileSize"];
            message.SoundLength = [result intForColumn:@"SoundLength"];
            message.FileCacheName = [result stringForColumn:@"FileCacheName"];
            message.FileName = [result stringForColumn:@"FileName"];
            message.ReadStatus = [result boolForColumn:@"ReadStatus"];
            message.SoundPlayStatus = [result intForColumn:@"SoundPlayStatus"];
            message.MessageSendedStatus = [result intForColumn:@"MessageSendedStatus"];
            message.GroupId = [result stringForColumn:@"GroupId"];
            message.SenderCode = [result stringForColumn:@"SenderCode"];
            message.TimeStamp = [result stringForColumn:@"TimeStamp"];
            message.FriendJid = [result stringForColumn:@"FriendJid"];
            message.MineJid = [result stringForColumn:@"MineJid"];
            message.MessageId = [result stringForColumn:@"MessageId"];
            message.FriendUserId = [result stringForColumn:@"FriendUserId"];
            message.MineUserId = [result stringForColumn:@"MineUserId"];
            message.isMineSend = [result boolForColumn:@"isMineSend"];
            message.FriendUserName = [result stringForColumn:@"FriendUserName"];
            message.MineUserName = [result stringForColumn:@"MineUserName"];
            message.IsShowInmessageList = [result boolForColumn:@"IsShowInmessageList"];
            message.Stream = [result stringForColumn:@"Stream"];
            message.SendStatus = [result stringForColumn:@"SendStatus"];
            
            message.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            message.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            message.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];

            
            [array addObject:message];
            message = nil;
        }
        [result close];
        //        count = [result columnCount];
    }];
    return array;
    
}

- (NSArray *)loadAllUnReadMessageWithMineUserId:(NSString *)MineUserId FriendUserId:(NSString *)FriendUserId
{
    
    if(M_ISSTRNULL(MineUserId) || M_ISSTRNULL(FriendUserId))
    {
        return nil;
    }
    
    
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];

    NSString *sql = nil;
    
    sql = [NSString stringWithFormat:@"select * from MESSAGE where FriendUserId=\'%@\' AND MineUserId =\'%@\' AND ReadStatus = 0 AND isMineSend = 0 order by TimeStamp  DESC",FriendUserId,MineUserId];
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            CMessageModel *message = [[CMessageModel alloc] init];
            message.MessageIndex = [result longLongIntForColumn:@"MessageIndex"];
            message.ServerID = [result stringForColumn:@"ServerID"];
            message.MessageType = [result stringForColumn:@"MessageType"];
            message.MessageContent = [result stringForColumn:@"MessageContent"];
            message.MessageUri = [result stringForColumn:@"MessageUri"];
            message.FileSize = [result stringForColumn:@"FileSize"];
            message.SoundLength = [result intForColumn:@"SoundLength"];
            message.FileCacheName = [result stringForColumn:@"FileCacheName"];
            message.FileName = [result stringForColumn:@"FileName"];
            message.ReadStatus = [result boolForColumn:@"ReadStatus"];
             message.SoundPlayStatus = [result intForColumn:@"SoundPlayStatus"];
            message.MessageSendedStatus = [result intForColumn:@"MessageSendedStatus"];
            message.GroupId = [result stringForColumn:@"GroupId"];
            message.SenderCode = [result stringForColumn:@"SenderCode"];
            message.TimeStamp = [result stringForColumn:@"TimeStamp"];
            message.FriendJid = [result stringForColumn:@"FriendJid"];
            message.MineJid = [result stringForColumn:@"MineJid"];
            message.MessageId = [result stringForColumn:@"MessageId"];
            message.FriendUserId = [result stringForColumn:@"FriendUserId"];
            message.MineUserId = [result stringForColumn:@"MineUserId"];
            message.isMineSend = [result boolForColumn:@"isMineSend"];
            message.FriendUserName = [result stringForColumn:@"FriendUserName"];
            message.MineUserName = [result stringForColumn:@"MineUserName"];
            message.IsShowInmessageList = [result boolForColumn:@"IsShowInmessageList"];
            message.Stream = [result stringForColumn:@"Stream"];
            message.SendStatus = [result stringForColumn:@"SendStatus"];
            
            message.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            message.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            message.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];

            
            
            [array addObject:message];
            message = nil;
        }
        [result close];
        //        count = [result columnCount];
    }];
    return array;

}
////加载消息消息
//-(NSArray *)loadMessages:(NSString *)fromUser GroupId:(NSString *)groupId beginIndex:(NSInteger)beginIndex messageCount:(NSInteger)messageCount
//{
//    
//    
//    if(M_ISSTRNULL(fromUser))
//    {
//        return nil;
//    }
//    
//    
//    NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];
//    
//    NSString *sql = nil;
//    
//    if(messageCount > 0)
//    {
//        sql = [NSString stringWithFormat:@"select * from Message where (SenderCode=\'%@\' AND (messageIndex < %d)  AND  GroupId =\'%@\' ) order by messageIndex  DESC limit %d offset 0", fromUser, beginIndex,M_ISSTRNULL(groupId)?@"":groupId, messageCount ];
//    }
//    
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
//    [queen inDatabase:^(FMDatabase *db) {
//        FMResultSet *result = [db executeQuery:sql];
//        while ([result next]) {
//            CMessageModel *message = [[CMessageModel alloc] init];
//            message.MessageIndex = [result longLongIntForColumn:@"MessageIndex"];
//            message.ServerID = [result stringForColumn:@"ServerID"];
//            message.MessageType = [result intForColumn:@"MessageType"];
//            message.MessageContent = [result stringForColumn:@"MessageContent"];
//            message.MessageUri = [result stringForColumn:@"MessageUri"];
//            message.FileSize = [result stringForColumn:@"FileSize"];
//            message.SoundLength = [result intForColumn:@"SoundLength"];
//            message.FileCacheName = [result stringForColumn:@"FileCacheName"];
//            message.FileName = [result stringForColumn:@"FileName"];
//            message.ReadStatus = [result boolForColumn:@"ReadStatus"];
//            message.SoundPlayStatus = [result boolForColumn:@"SoundPlayStatus"];
//            message.MessageSendedStatus = [result intForColumn:@"MessageSendedStatus"];
//            message.GroupId = [result stringForColumn:@"GroupId"];
//            message.SenderCode = [result stringForColumn:@"SenderCode"];
//            message.timeStamp = [result stringForColumn:@"TimeStamp"];
//            message.FriendJid = [result stringForColumn:@"FriendJid"];
//            message.MineJid = [result stringForColumn:@"MineJid"];
//            message.messageId = [result stringForColumn:@"MessageId"];
//            message.FriendUserId = [result stringForColumn:@"FriendUserId"];
//            message.MineUserId = [result stringForColumn:@"MineUserId"];
//            message.isMineSend = [result boolForColumn:@"isMineSend"];
//            [array addObject:message];
//            message = nil;
//        }
//    }];
//    return array;
//}



- (CMessageModel *)loadGroupMineSendMaxMessageIndexWithGroupId:(NSString *)groupId
{
    if(M_ISSTRNULL(groupId))
    {
        return nil;
    }
    
    NSString *sql = nil;
    
    sql = [NSString stringWithFormat:@"select * from MESSAGE where MessageIndex = (select max(MessageIndex) from MESSAGE where GroupId =\'%@\' AND isMineSend = 1)",groupId];
    __block CMessageModel *message = nil;

//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            message = [[CMessageModel alloc] init];
            message.MessageIndex = [result longLongIntForColumn:@"MessageIndex"];
            message.ServerID = [result stringForColumn:@"ServerID"];
            message.MessageType = [result stringForColumn:@"MessageType"];
            message.MessageContent = [result stringForColumn:@"MessageContent"];
            message.MessageUri = [result stringForColumn:@"MessageUri"];
            message.FileSize = [result stringForColumn:@"FileSize"];
            message.SoundLength = [result intForColumn:@"SoundLength"];
            message.FileCacheName = [result stringForColumn:@"FileCacheName"];
            message.FileName = [result stringForColumn:@"FileName"];
            message.ReadStatus = [result boolForColumn:@"ReadStatus"];
             message.SoundPlayStatus = [result intForColumn:@"SoundPlayStatus"];
            message.MessageSendedStatus = [result intForColumn:@"MessageSendedStatus"];
            message.GroupId = [result stringForColumn:@"GroupId"];
            message.SenderCode = [result stringForColumn:@"SenderCode"];
            message.TimeStamp = [result stringForColumn:@"TimeStamp"];
            message.FriendJid = [result stringForColumn:@"FriendJid"];
            message.MineJid = [result stringForColumn:@"MineJid"];
            message.MessageId = [result stringForColumn:@"MessageId"];
            message.FriendUserId = [result stringForColumn:@"FriendUserId"];
            message.MineUserId = [result stringForColumn:@"MineUserId"];
            message.isMineSend = [result boolForColumn:@"isMineSend"];
            message.FriendUserName = [result stringForColumn:@"FriendUserName"];
            message.MineUserName = [result stringForColumn:@"MineUserName"];
            message.IsShowInmessageList = [result boolForColumn:@"IsShowInmessageList"];
            message.Stream = [result stringForColumn:@"Stream"];
            message.SendStatus = [result stringForColumn:@"SendStatus"];
            
            message.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            message.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            message.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];

            
            }
        [result close];
        //        count = [result columnCount];
    }];
    return message;

}
- (CMessageModel *)loadMineSendMaxMessageIndexWithMineUserId:(NSString *)MineUserId FriendUserId:(NSString *)FriendUserId
{
    if(M_ISSTRNULL(MineUserId) || M_ISSTRNULL(FriendUserId))
    {
        return nil;
    }
    
    NSString *sql = nil;
    
    sql = [NSString stringWithFormat:@"select * from MESSAGE where MessageIndex = (select max(MessageIndex) from MESSAGE where FriendUserId=\'%@\' AND MineUserId =\'%@\' AND isMineSend = 1)",FriendUserId,MineUserId];
    __block CMessageModel *message = nil;
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
           message = [[CMessageModel alloc] init];
            message.MessageIndex = [result longLongIntForColumn:@"MessageIndex"];
            message.ServerID = [result stringForColumn:@"ServerID"];
            message.MessageType = [result stringForColumn:@"MessageType"];
            message.MessageContent = [result stringForColumn:@"MessageContent"];
            message.MessageUri = [result stringForColumn:@"MessageUri"];
            message.FileSize = [result stringForColumn:@"FileSize"];
            message.SoundLength = [result intForColumn:@"SoundLength"];
            message.FileCacheName = [result stringForColumn:@"FileCacheName"];
            message.FileName = [result stringForColumn:@"FileName"];
            message.ReadStatus = [result boolForColumn:@"ReadStatus"];
             message.SoundPlayStatus = [result intForColumn:@"SoundPlayStatus"];
            message.MessageSendedStatus = [result intForColumn:@"MessageSendedStatus"];
            message.GroupId = [result stringForColumn:@"GroupId"];
            message.SenderCode = [result stringForColumn:@"SenderCode"];
            message.TimeStamp = [result stringForColumn:@"TimeStamp"];
            message.FriendJid = [result stringForColumn:@"FriendJid"];
            message.MineJid = [result stringForColumn:@"MineJid"];
            message.MessageId = [result stringForColumn:@"MessageId"];
            message.FriendUserId = [result stringForColumn:@"FriendUserId"];
            message.MineUserId = [result stringForColumn:@"MineUserId"];
            message.isMineSend = [result boolForColumn:@"isMineSend"];
            message.FriendUserName = [result stringForColumn:@"FriendUserName"];
            message.MineUserName = [result stringForColumn:@"MineUserName"];
            message.IsShowInmessageList = [result boolForColumn:@"IsShowInmessageList"];
            message.Stream = [result stringForColumn:@"Stream"];
            message.SendStatus = [result stringForColumn:@"SendStatus"];
            
            
            message.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            message.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            message.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];

            
        }
        [result close];
        //        count = [result columnCount];
    }];
    return message;

}

- (NSArray *)loadGroupAllIsSendingMessageWithGroupId:(NSString *)groupId
{
    if(M_ISSTRNULL(groupId))
    {
        return nil;
    }
    
    
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];
    
    NSString *sql = nil;
    
    sql = [NSString stringWithFormat:@"select * from MESSAGE where GroupId =\'%@\' AND isMineSend = 1 AND MessageSendedStatus = 2 order by TimeStamp  ASC",groupId];
    
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            CMessageModel *message = [[CMessageModel alloc] init];
            message.MessageIndex = [result longLongIntForColumn:@"MessageIndex"];
            message.ServerID = [result stringForColumn:@"ServerID"];
            message.MessageType = [result stringForColumn:@"MessageType"];
            message.MessageContent = [result stringForColumn:@"MessageContent"];
            message.MessageUri = [result stringForColumn:@"MessageUri"];
            message.FileSize = [result stringForColumn:@"FileSize"];
            message.SoundLength = [result intForColumn:@"SoundLength"];
            message.FileCacheName = [result stringForColumn:@"FileCacheName"];
            message.FileName = [result stringForColumn:@"FileName"];
            message.ReadStatus = [result boolForColumn:@"ReadStatus"];
             message.SoundPlayStatus = [result intForColumn:@"SoundPlayStatus"];
            message.MessageSendedStatus = [result intForColumn:@"MessageSendedStatus"];
            message.GroupId = [result stringForColumn:@"GroupId"];
            message.SenderCode = [result stringForColumn:@"SenderCode"];
            message.TimeStamp = [result stringForColumn:@"TimeStamp"];
            message.FriendJid = [result stringForColumn:@"FriendJid"];
            message.MineJid = [result stringForColumn:@"MineJid"];
            message.MessageId = [result stringForColumn:@"MessageId"];
            message.FriendUserId = [result stringForColumn:@"FriendUserId"];
            message.MineUserId = [result stringForColumn:@"MineUserId"];
            message.isMineSend = [result boolForColumn:@"isMineSend"];
            message.FriendUserName = [result stringForColumn:@"FriendUserName"];
            message.MineUserName = [result stringForColumn:@"MineUserName"];
            message.IsShowInmessageList = [result boolForColumn:@"IsShowInmessageList"];
            message.Stream = [result stringForColumn:@"Stream"];
            message.SendStatus = [result stringForColumn:@"SendStatus"];
            
            message.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            message.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            message.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];

            
            [array addObject:message];
            message = nil;
        }
        [result close];
        //        count = [result columnCount];
    }];
    return array;

}
- (NSArray *)loadAllIsSendingMessageWithMineUserId:(NSString *)MineUserId FriendUserId:(NSString *)FriendUserId
{
    if(M_ISSTRNULL(MineUserId) || M_ISSTRNULL(FriendUserId))
    {
        return nil;
    }
    
    
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];
    
    NSString *sql = nil;
    
    sql = [NSString stringWithFormat:@"select * from MESSAGE where FriendUserId=\'%@\' AND MineUserId =\'%@\' AND isMineSend = 1 AND MessageSendedStatus = 2 order by TimeStamp ASC",FriendUserId,MineUserId];
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            CMessageModel *message = [[CMessageModel alloc] init];
            message.MessageIndex = [result longLongIntForColumn:@"MessageIndex"];
            message.ServerID = [result stringForColumn:@"ServerID"];
            message.MessageType = [result stringForColumn:@"MessageType"];
            message.MessageContent = [result stringForColumn:@"MessageContent"];
            message.MessageUri = [result stringForColumn:@"MessageUri"];
            message.FileSize = [result stringForColumn:@"FileSize"];
            message.SoundLength = [result intForColumn:@"SoundLength"];
            message.FileCacheName = [result stringForColumn:@"FileCacheName"];
            message.FileName = [result stringForColumn:@"FileName"];
            message.ReadStatus = [result boolForColumn:@"ReadStatus"];
             message.SoundPlayStatus = [result intForColumn:@"SoundPlayStatus"];
            message.MessageSendedStatus = [result intForColumn:@"MessageSendedStatus"];
            message.GroupId = [result stringForColumn:@"GroupId"];
            message.SenderCode = [result stringForColumn:@"SenderCode"];
            message.TimeStamp = [result stringForColumn:@"TimeStamp"];
            message.FriendJid = [result stringForColumn:@"FriendJid"];
            message.MineJid = [result stringForColumn:@"MineJid"];
            message.MessageId = [result stringForColumn:@"MessageId"];
            message.FriendUserId = [result stringForColumn:@"FriendUserId"];
            message.MineUserId = [result stringForColumn:@"MineUserId"];
            message.isMineSend = [result boolForColumn:@"isMineSend"];
            message.FriendUserName = [result stringForColumn:@"FriendUserName"];
            message.MineUserName = [result stringForColumn:@"MineUserName"];
            message.IsShowInmessageList = [result boolForColumn:@"IsShowInmessageList"];
            message.Stream = [result stringForColumn:@"Stream"];
            message.SendStatus = [result stringForColumn:@"SendStatus"];
            
            message.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            message.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            message.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];

            
            [array addObject:message];
            message = nil;
        }
        [result close];
        //        count = [result columnCount];
    }];
    return array;

}


- (CMessageModel *)loadMinUnReadMessageWithMineUserId:(NSString *)MineUserId FriendUserId:(NSString *)FriendUserId
{
    if(M_ISSTRNULL(MineUserId) || M_ISSTRNULL(FriendUserId))
    {
        return nil;
    }
    
    NSString *sql = nil;
    
    sql = [NSString stringWithFormat:@"select * from MESSAGE where TimeStamp = (select min(TimeStamp) from MESSAGE where FriendUserId=\'%@\' AND MineUserId =\'%@\' AND isMineSend = 0  AND ReadStatus = 0)",FriendUserId,MineUserId];
    __block CMessageModel *message = nil;
//queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            message = [[CMessageModel alloc] init];
            message.MessageIndex = [result longLongIntForColumn:@"MessageIndex"];
            message.ServerID = [result stringForColumn:@"ServerID"];
            message.MessageType = [result stringForColumn:@"MessageType"];
            message.MessageContent = [result stringForColumn:@"MessageContent"];
            message.MessageUri = [result stringForColumn:@"MessageUri"];
            message.FileSize = [result stringForColumn:@"FileSize"];
            message.SoundLength = [result intForColumn:@"SoundLength"];
            message.FileCacheName = [result stringForColumn:@"FileCacheName"];
            message.FileName = [result stringForColumn:@"FileName"];
            message.ReadStatus = [result boolForColumn:@"ReadStatus"];
             message.SoundPlayStatus = [result intForColumn:@"SoundPlayStatus"];
            message.MessageSendedStatus = [result intForColumn:@"MessageSendedStatus"];
            message.GroupId = [result stringForColumn:@"GroupId"];
            message.SenderCode = [result stringForColumn:@"SenderCode"];
            message.TimeStamp = [result stringForColumn:@"TimeStamp"];
            message.FriendJid = [result stringForColumn:@"FriendJid"];
            message.MineJid = [result stringForColumn:@"MineJid"];
            message.MessageId = [result stringForColumn:@"MessageId"];
            message.FriendUserId = [result stringForColumn:@"FriendUserId"];
            message.MineUserId = [result stringForColumn:@"MineUserId"];
            message.isMineSend = [result boolForColumn:@"isMineSend"];
            message.FriendUserName = [result stringForColumn:@"FriendUserName"];
            message.MineUserName = [result stringForColumn:@"MineUserName"];
            message.IsShowInmessageList = [result boolForColumn:@"IsShowInmessageList"];
            message.Stream = [result stringForColumn:@"Stream"];
            message.SendStatus = [result stringForColumn:@"SendStatus"];
            
            
            message.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            message.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            message.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];

        }
        [result close];
        //        count = [result columnCount];
    }];
    return message;

}


- (NSArray *)loadAllMessageGroupId
{
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];
    
    NSString *sql = nil;
    
    __block NSString *groupId = nil;
    sql = [NSString stringWithFormat:@"select GroupId from MESSAGE where TimeStamp in (select max(TimeStamp) as TimeStamp from MESSAGE group by GroupId)"];
    //    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            groupId = [result stringForColumn:@"GroupId"];
            if (!M_ISSTRNULL(groupId)) {
                [array addObject:groupId];
            }
            groupId = nil;
        }
        [result close];
        //        count = [result columnCount];
    }];
    return array;

}

- (CMessageModel *)loadGroupMinUnReadMessageWithGroupId:(NSString *)groupId
{
    if(M_ISSTRNULL(groupId))
    {
        return nil;
    }
    
    NSString *sql = nil;
    
    sql = [NSString stringWithFormat:@"select * from MESSAGE where TimeStamp = (select min(TimeStamp) from MESSAGE where GroupId =\'%@\' AND isMineSend = 0  AND ReadStatus = 0)",groupId];
    __block CMessageModel *message = nil;
    
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            message = [[CMessageModel alloc] init];
            message.MessageIndex = [result longLongIntForColumn:@"MessageIndex"];
            message.ServerID = [result stringForColumn:@"ServerID"];
            message.MessageType = [result stringForColumn:@"MessageType"];
            message.MessageContent = [result stringForColumn:@"MessageContent"];
            message.MessageUri = [result stringForColumn:@"MessageUri"];
            message.FileSize = [result stringForColumn:@"FileSize"];
            message.SoundLength = [result intForColumn:@"SoundLength"];
            message.FileCacheName = [result stringForColumn:@"FileCacheName"];
            message.FileName = [result stringForColumn:@"FileName"];
            message.ReadStatus = [result boolForColumn:@"ReadStatus"];
             message.SoundPlayStatus = [result intForColumn:@"SoundPlayStatus"];
            message.MessageSendedStatus = [result intForColumn:@"MessageSendedStatus"];
            message.GroupId = [result stringForColumn:@"GroupId"];
            message.SenderCode = [result stringForColumn:@"SenderCode"];
            message.TimeStamp = [result stringForColumn:@"TimeStamp"];
            message.FriendJid = [result stringForColumn:@"FriendJid"];
            message.MineJid = [result stringForColumn:@"MineJid"];
            message.MessageId = [result stringForColumn:@"MessageId"];
            message.FriendUserId = [result stringForColumn:@"FriendUserId"];
            message.MineUserId = [result stringForColumn:@"MineUserId"];
            message.isMineSend = [result boolForColumn:@"isMineSend"];
            message.FriendUserName = [result stringForColumn:@"FriendUserName"];
            message.MineUserName = [result stringForColumn:@"MineUserName"];
            message.IsShowInmessageList = [result boolForColumn:@"IsShowInmessageList"];
            message.Stream = [result stringForColumn:@"Stream"];
            message.SendStatus = [result stringForColumn:@"SendStatus"];
            
            message.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            message.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            message.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];

            
        }
        [result close];
        //        count = [result columnCount];
    }];
    return message;

    
}
- (BOOL)updateMessageReadStatusWithMessageId:(NSString *)messageId
{
    if(M_ISSTRNULL(messageId))
    {
        return NO;
    }
    __block BOOL ret = NO;
//    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
//        ret = [db executeUpdate:@"UPDATE MESSAGE SET  ReadStatus = 1 where MessageId = ? ",M_STOREVALUE(messageId)];
//    }];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        NSString *str = [messageId stringByAppendingString:@"%"];
        ret = [db executeUpdate:@"UPDATE MESSAGE SET  ReadStatus = 1 where MessageId LIKE ?",str];
    }];
    return ret;
}
-(BOOL)updateMessageSoundStatusWithMessageId:(NSString *)messageId
{
    if(M_ISSTRNULL(messageId))
    {
        return NO;
    }

    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"UPDATE MESSAGE SET  SoundPlayStatus = ? where MessageId = ? ",@(MessageSoundPlayed),M_STOREVALUE(messageId)];
    }];
    return ret;
}

- (NSArray *)updateMessageStatuIsSendingToFailed
{
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];
    
    __block NSString *sql = nil;
    
    sql = [NSString stringWithFormat:@"select MessageId from MESSAGE where MessageSendedStatus = 2 AND isMineSend = 1"];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            
            [array addObject:[result stringForColumn:@"MessageId"]];
        }
        [result close];
        
    }];
    return array;

}
- (BOOL)batchUpdateStatuWithIndeList:(NSArray *)indexList
{
    if(indexList == nil || [indexList count] <= 0)
    {
        return NO;
    }
  
    __block BOOL ret = NO;
    
    
        [[FMShareDataBase shareDataBase].shareQueue inTransaction:^(FMDatabase *db, BOOL *rollback) {
            for(NSString *messageId in indexList)
            {
                ret = [db executeUpdate:@"UPDATE MESSAGE SET MessageSendedStatus = 4 where MessageId = ?",messageId];
                if(!ret)
                {
                    *rollback = YES;
                }
            }
            
        }];
   
    return ret;

}

//- (BOOL)batchUpdateReadStatusWithIndeList:(NSArray *)indexList
//{
//    if(indexList == nil || [indexList count] <= 0)
//    {
//        return NO;
//    }
//    
//    __block BOOL ret = NO;
//    
//    
//    [[FMShareDataBase shareDataBase].shareQueue inTransaction:^(FMDatabase *db, BOOL *rollback) {
//        for(NSNumber *messageIndex in indexList)
//        {
//            ret = [db executeUpdate:@"UPDATE MESSAGE SET ReadStatus = 1 where MessageIndex = ?",messageIndex];
//            if(!ret)
//            {
//                *rollback = YES;
//            }
//        }
//        
//    }];
//    
//    return ret;
//    
//}

- (CMessageModel *)loadMessageWithMessageId:(NSString *)messageId
{
    if(M_ISSTRNULL(messageId))
    {
        return nil;
    }
    NSString *sql = nil;
    __block CMessageModel *message = nil;
    sql = [NSString stringWithFormat:@"select * from MESSAGE where  MessageId = \'%@\'", messageId];
    //    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            message = [[CMessageModel alloc] init];
            message.MessageIndex = [result longLongIntForColumn:@"MessageIndex"];
            message.ServerID = [result stringForColumn:@"ServerID"];
            message.MessageType = [result stringForColumn:@"MessageType"];
            message.MessageContent = [result stringForColumn:@"MessageContent"];
            message.MessageUri = [result stringForColumn:@"MessageUri"];
            message.FileSize = [result stringForColumn:@"FileSize"];
            message.SoundLength = [result intForColumn:@"SoundLength"];
            message.FileCacheName = [result stringForColumn:@"FileCacheName"];
            message.FileName = [result stringForColumn:@"FileName"];
            message.ReadStatus = [result boolForColumn:@"ReadStatus"];
            message.SoundPlayStatus = [result intForColumn:@"SoundPlayStatus"];
            message.MessageSendedStatus = [result intForColumn:@"MessageSendedStatus"];
            message.GroupId = [result stringForColumn:@"GroupId"];
            message.SenderCode = [result stringForColumn:@"SenderCode"];
            message.TimeStamp = [result stringForColumn:@"TimeStamp"];
            message.FriendJid = [result stringForColumn:@"FriendJid"];
            message.MineJid = [result stringForColumn:@"MineJid"];
            message.MessageId = [result stringForColumn:@"MessageId"];
            message.FriendUserId = [result stringForColumn:@"FriendUserId"];
            message.MineUserId = [result stringForColumn:@"MineUserId"];
            message.isMineSend = [result boolForColumn:@"isMineSend"];
            message.FriendUserName = [result stringForColumn:@"FriendUserName"];
            message.MineUserName = [result stringForColumn:@"MineUserName"];
            message.IsShowInmessageList = [result boolForColumn:@"IsShowInmessageList"];
            message.Stream = [result stringForColumn:@"Stream"];
            message.SendStatus = [result stringForColumn:@"SendStatus"];
            
            
            message.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            message.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            message.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];

        }
        [result close];
    }];
    return message;

}

//搜索消息
- (NSArray *)searchMessageWithSearchStr:(NSString *)searchStr groupId:(NSString *)groupId
{
    if (M_ISSTRNULL(groupId) || M_ISSTRNULL(searchStr)) {
        return nil;
    }
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];
    
    NSString *sql = nil;
    
    sql = [NSString stringWithFormat:@"select * from MESSAGE where MessageType = \'%@\' AND GroupId =\'%@\' AND (MessageContent LIKE '%%%@%%' ) order by TimeStamp ASC",@"text",groupId,searchStr];
    //    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            CMessageModel *message = [[CMessageModel alloc] init];
            message.MessageIndex = [result longLongIntForColumn:@"MessageIndex"];
            message.ServerID = [result stringForColumn:@"ServerID"];
            message.MessageType = [result stringForColumn:@"MessageType"];
            message.MessageContent = [result stringForColumn:@"MessageContent"];
            message.MessageUri = [result stringForColumn:@"MessageUri"];
            message.FileSize = [result stringForColumn:@"FileSize"];
            message.SoundLength = [result intForColumn:@"SoundLength"];
            message.FileCacheName = [result stringForColumn:@"FileCacheName"];
            message.FileName = [result stringForColumn:@"FileName"];
            message.ReadStatus = [result boolForColumn:@"ReadStatus"];
            message.SoundPlayStatus = [result intForColumn:@"SoundPlayStatus"];
            message.MessageSendedStatus = [result intForColumn:@"MessageSendedStatus"];
            message.GroupId = [result stringForColumn:@"GroupId"];
            message.SenderCode = [result stringForColumn:@"SenderCode"];
            message.TimeStamp = [result stringForColumn:@"TimeStamp"];
            message.FriendJid = [result stringForColumn:@"FriendJid"];
            message.MineJid = [result stringForColumn:@"MineJid"];
            message.MessageId = [result stringForColumn:@"MessageId"];
            message.FriendUserId = [result stringForColumn:@"FriendUserId"];
            message.MineUserId = [result stringForColumn:@"MineUserId"];
            message.isMineSend = [result boolForColumn:@"isMineSend"];
            message.FriendUserName = [result stringForColumn:@"FriendUserName"];
            message.MineUserName = [result stringForColumn:@"MineUserName"];
            message.IsShowInmessageList = [result boolForColumn:@"IsShowInmessageList"];
            message.Stream = [result stringForColumn:@"Stream"];
            message.SendStatus = [result stringForColumn:@"SendStatus"];
            
            message.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            message.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            message.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];

            
            [array addObject:message];
            message = nil;
        }
        [result close];
        //        count = [result columnCount];
    }];
    return array;

}
- (NSArray *)searchMessageWithSearchStr:(NSString *)searchStr mineId:(NSString *)mineId friendId:(NSString *)friendId
{
    if (M_ISSTRNULL(mineId) || M_ISSTRNULL(friendId) || M_ISSTRNULL(searchStr)) {
        return nil;
    }
    
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];
    
    NSString *sql = nil;
    
    sql = [NSString stringWithFormat:@"select * from MESSAGE where MessageType = \'%@\' AND FriendUserId=\'%@\' AND MineUserId =\'%@\' AND (MessageContent LIKE '%%%@%%') order by TimeStamp ASC",@"text",friendId,mineId,searchStr];
    //    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            CMessageModel *message = [[CMessageModel alloc] init];
            message.MessageIndex = [result longLongIntForColumn:@"MessageIndex"];
            message.ServerID = [result stringForColumn:@"ServerID"];
            message.MessageType = [result stringForColumn:@"MessageType"];
            message.MessageContent = [result stringForColumn:@"MessageContent"];
            message.MessageUri = [result stringForColumn:@"MessageUri"];
            message.FileSize = [result stringForColumn:@"FileSize"];
            message.SoundLength = [result intForColumn:@"SoundLength"];
            message.FileCacheName = [result stringForColumn:@"FileCacheName"];
            message.FileName = [result stringForColumn:@"FileName"];
            message.ReadStatus = [result boolForColumn:@"ReadStatus"];
            message.SoundPlayStatus = [result intForColumn:@"SoundPlayStatus"];
            message.MessageSendedStatus = [result intForColumn:@"MessageSendedStatus"];
            message.GroupId = [result stringForColumn:@"GroupId"];
            message.SenderCode = [result stringForColumn:@"SenderCode"];
            message.TimeStamp = [result stringForColumn:@"TimeStamp"];
            message.FriendJid = [result stringForColumn:@"FriendJid"];
            message.MineJid = [result stringForColumn:@"MineJid"];
            message.MessageId = [result stringForColumn:@"MessageId"];
            message.FriendUserId = [result stringForColumn:@"FriendUserId"];
            message.MineUserId = [result stringForColumn:@"MineUserId"];
            message.isMineSend = [result boolForColumn:@"isMineSend"];
            message.FriendUserName = [result stringForColumn:@"FriendUserName"];
            message.MineUserName = [result stringForColumn:@"MineUserName"];
            message.IsShowInmessageList = [result boolForColumn:@"IsShowInmessageList"];
            message.Stream = [result stringForColumn:@"Stream"];
            message.SendStatus = [result stringForColumn:@"SendStatus"];
            message.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            message.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            message.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];

            
            [array addObject:message];
            message = nil;
        }
        [result close];
        //        count = [result columnCount];
    }];
    return array;

}
//搜索所有消息
- (NSArray*)searchMessageWithSearchStr:(NSString *)searchStr
{
    if (M_ISSTRNULL(searchStr)) {
        return nil;
    }
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];
    
    NSString *sql = nil;
    
    sql = [NSString stringWithFormat:@"select * from MESSAGE where MessageType = \'%@\' AND (MessageContent LIKE '%%%@%%' ) order by TimeStamp ASC",@"text",searchStr];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            CMessageModel *message = [[CMessageModel alloc] init];
            message.MessageIndex = [result longLongIntForColumn:@"MessageIndex"];
            message.ServerID = [result stringForColumn:@"ServerID"];
            message.MessageType = [result stringForColumn:@"MessageType"];
            message.MessageContent = [result stringForColumn:@"MessageContent"];
            message.MessageUri = [result stringForColumn:@"MessageUri"];
            message.FileSize = [result stringForColumn:@"FileSize"];
            message.SoundLength = [result intForColumn:@"SoundLength"];
            message.FileCacheName = [result stringForColumn:@"FileCacheName"];
            message.FileName = [result stringForColumn:@"FileName"];
            message.ReadStatus = [result boolForColumn:@"ReadStatus"];
            message.SoundPlayStatus = [result intForColumn:@"SoundPlayStatus"];
            message.MessageSendedStatus = [result intForColumn:@"MessageSendedStatus"];
            message.GroupId = [result stringForColumn:@"GroupId"];
            message.SenderCode = [result stringForColumn:@"SenderCode"];
            message.TimeStamp = [result stringForColumn:@"TimeStamp"];
            message.FriendJid = [result stringForColumn:@"FriendJid"];
            message.MineJid = [result stringForColumn:@"MineJid"];
            message.MessageId = [result stringForColumn:@"MessageId"];
            message.FriendUserId = [result stringForColumn:@"FriendUserId"];
            message.MineUserId = [result stringForColumn:@"MineUserId"];
            message.isMineSend = [result boolForColumn:@"isMineSend"];
            message.FriendUserName = [result stringForColumn:@"FriendUserName"];
            message.MineUserName = [result stringForColumn:@"MineUserName"];
            message.IsShowInmessageList = [result boolForColumn:@"IsShowInmessageList"];
            message.Stream = [result stringForColumn:@"Stream"];
            message.SendStatus = [result stringForColumn:@"SendStatus"];
            message.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            message.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            message.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];
            [array addObject:message];
            message = nil;
        }
        [result close];
        //        count = [result columnCount];
    }];
    return array;
    
}

//查询所有未读消息id
- (NSArray *)loadAllUnReadMessgeID
{
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];
    
    NSString *sql = nil;
    
    sql = [NSString stringWithFormat:@"select MessageId,MessageType,isMineSend from MESSAGE where ReadStatus = 0"];
    //    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            CMessageModel *message = [[CMessageModel alloc] init];
            message.MessageType = [result stringForColumn:@"MessageType"];
            message.MessageId = [result stringForColumn:@"MessageId"];
            message.isMineSend = [result boolForColumn:@"isMineSend"];
            [array addObject:message];
            message = nil;
        }
        [result close];
        //        count = [result columnCount];
    }];
    return array;

}
//将所有消息更新为已读
- (BOOL)updateAllMessageReadSatuWithIdArray:(NSArray *)idArray
{
    if(idArray == nil || [idArray count] <= 0)
    {
        return NO;
    }
    
    __block BOOL ret = NO;
    
    
    [[FMShareDataBase shareDataBase].shareQueue inTransaction:^(FMDatabase *db, BOOL *rollback) {
        for(CMessageModel *mesId in idArray)
        {
            ret = [db executeUpdate:@"UPDATE MESSAGE SET ReadStatus = 1 where MessageId = ?",mesId.MessageId];
            if(!ret)
            {
                *rollback = YES;
            }
        }
        
    }];
    
    return ret;

}

- (BOOL)updateMessageStatuIsSendingToFailedWithArray:(NSArray *)array
{
    if(array == nil || [array count] <= 0)
    {
        return NO;
    }
    //    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    
    
    [[FMShareDataBase shareDataBase].shareQueue inTransaction:^(FMDatabase *db, BOOL *rollback) {
        for(NSString *mesId in array)
        {
            ret = [db executeUpdate:@"UPDATE MESSAGE SET MessageSendedStatus = 4 where MessageId = ?",mesId];
            if(!ret)
            {
                *rollback = YES;
            }
        }
        
    }];
    return ret;
    
}

- (BOOL)updateMessageStatuIsSendingToFailedWithMessageId:(NSString *)messageId
{
    if (M_ISSTRNULL(messageId)) {
        return NO;
    }
    __block BOOL ret = NO;
    
    
    [[FMShareDataBase shareDataBase].shareQueue inTransaction:^(FMDatabase *db, BOOL *rollback) {
        ret = [db executeUpdate:@"UPDATE MESSAGE SET MessageSendedStatus = 4 where MessageId = ?",messageId];
        
    }];
    return ret;

}
- (NSArray *)LoadMessageStatuIsSending
{
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];
    
    __block NSString *sql = nil;
    
    sql = [NSString stringWithFormat:@"select * from MESSAGE where MessageSendedStatus = 2 AND isMineSend = 1"];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            
            CMessageModel *message = [[CMessageModel alloc] init];
            message.MessageIndex = [result longLongIntForColumn:@"MessageIndex"];
            message.ServerID = [result stringForColumn:@"ServerID"];
            message.MessageType = [result stringForColumn:@"MessageType"];
            message.MessageContent = [result stringForColumn:@"MessageContent"];
            message.MessageUri = [result stringForColumn:@"MessageUri"];
            message.FileSize = [result stringForColumn:@"FileSize"];
            message.SoundLength = [result intForColumn:@"SoundLength"];
            message.FileCacheName = [result stringForColumn:@"FileCacheName"];
            message.FileName = [result stringForColumn:@"FileName"];
            message.ReadStatus = [result boolForColumn:@"ReadStatus"];
            message.SoundPlayStatus = [result intForColumn:@"SoundPlayStatus"];
            message.MessageSendedStatus = [result intForColumn:@"MessageSendedStatus"];
            message.GroupId = [result stringForColumn:@"GroupId"];
            message.SenderCode = [result stringForColumn:@"SenderCode"];
            message.TimeStamp = [result stringForColumn:@"TimeStamp"];
            message.FriendJid = [result stringForColumn:@"FriendJid"];
            message.MineJid = [result stringForColumn:@"MineJid"];
            message.MessageId = [result stringForColumn:@"MessageId"];
            message.FriendUserId = [result stringForColumn:@"FriendUserId"];
            message.MineUserId = [result stringForColumn:@"MineUserId"];
            message.isMineSend = [result boolForColumn:@"isMineSend"];
            message.FriendUserName = [result stringForColumn:@"FriendUserName"];
            message.MineUserName = [result stringForColumn:@"MineUserName"];
            message.IsShowInmessageList = [result boolForColumn:@"IsShowInmessageList"];
            message.Stream = [result stringForColumn:@"Stream"];
            message.SendStatus = [result stringForColumn:@"SendStatus"];
            message.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            message.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            message.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];
            
            
            [array addObject:message];
            message = nil;
        }
        [result close];
        
    }];
    return array;

}

@end
